﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SRCBQuestionnaireStatistic.Model;

namespace SRCBQuestionnaireStatistic
{
    public static class StatisticBasisPrompt
    {
        public const string OkClicked = "OkClicked";
        public const string AllClicked = "AllClicked";
        private static Form prompt = null;
        public static Form CurrentOpeningForm { get { return prompt; } }
        public static bool CurrentOpeningForm_UserClickedOKButton { get { return (bool)prompt.Tag; } }
        public static string ShowDialog(List<Question> questions, string text, string caption, int width, int height)
        {
            prompt = new Form();
            prompt.Width = width;
            prompt.Height = height;
            prompt.Text = caption;
            prompt.FormClosing += new FormClosingEventHandler(prompt_FormClosing);
            prompt.Tag = ""; // "" means user click "X" to close;
            
            Panel questionPanel = null;
            int controlY = 30;
            int controlWidth = 150;
            int controlHeight = 20;
            int x = 10;
            int y = 10;
            System.Windows.Forms.RadioButton answerRadio = null;
            for (int i = 0; i < questions.Count; i++)
            {
                int panelHeight = questions[i].Height;

                questionPanel = new System.Windows.Forms.Panel();
                questionPanel.SetBounds(x, y, width, panelHeight);
                questionPanel.Show();
                //questionPanel.BackColor = System.Drawing.Color.Yellow;
                prompt.Controls.Add(questionPanel);

                int controlX = 0;
                int count = questions[i].AvailableAnswers.Count;
                for (int index = 0; index < count; index++)
                {
                    answerRadio = new System.Windows.Forms.RadioButton();
                    answerRadio.Show();
                    answerRadio.Name = "answer." + i.ToString() + "." + index.ToString();
                    answerRadio.Text = questions[i].AvailableAnswers[index].FullText.ToString();
                    
                    //if (isLongText)
                    //{
                    //    controlWidth = this.splitContainer2.Width;
                    //    controlX = 0;
                    //}

                    answerRadio.SetBounds(controlX, controlY, controlWidth, controlHeight);

                    //if (answers != null)
                    //{
                    //    if (answerRadio.Text.StartsWith(answers[i]))
                    //    {
                    //        answerRadio.Checked = true;
                    //    }
                    //}
                    //else
                    //{
                    //    if (index == 0)
                    //    {
                    //        answerRadio.Checked = true;
                    //    }
                    //}
                    
                    answerRadio.CheckedChanged += new System.EventHandler(answerRadio_CheckedChanged);
                    //answerRadio.GotFocus += new EventHandler(answerRadio_GotFocus);
                    questionPanel.Controls.Add(answerRadio);
                    if (index == 0)
                    {
                        answerRadio.Checked = true;
                    }
                    //if (isLongText)
                    //{
                    //    controlY += 30;
                    //}
                    //else
                    {
                        controlX += answerRadio.Width + 20;
                    }
                }
                y += questionPanel.Height;
            }
            Button confirmation = new Button() { Text = "Ok", Left = prompt.Left + prompt.Width - 120, Width = 80, Top = prompt.Top + prompt.Height - 60 };
            confirmation.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            //confirmation.Dock = DockStyle.Bottom;
            confirmation.Click += (sender, e) => {
                prompt.Tag += OkClicked + ";";
                prompt.Close();
            };
            prompt.Controls.Add(confirmation);

            Button all = new Button() { Text = "全员统计", Left = prompt.Left + 20, Width = 80, Top = prompt.Top + prompt.Height - 60 };
            all.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            all.Click += (sender, e) => {
                prompt.Tag = AllClicked;
                prompt.Close();
            };

            prompt.Controls.Add(all);

            prompt.ShowDialog();
            return (string)prompt.Tag;
        }

        private static void prompt_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form form = (Form)sender;
            if (!((string)form.Tag).Contains(OkClicked))
            {
                if (string.Equals(form.Tag.ToString(), AllClicked))
                {
                    form.Tag = AllClicked;
                }
            }
        }

        private static void answerRadio_CheckedChanged(object sender, EventArgs e)
        {
            Form form = (Form)((Control)sender).Parent.Parent;
            form.Tag = ((Control)sender).Text;
        }
    }
}
